/**
 * Copyright 2009 by Christian Scheid. This file is part of the switch eclipse plugin and is licensed under the GPL
 * version 3. Please refer to the URL http://www.gnu.org/licenses/gpl-3.0.html for details.
 */
package org.digitalsoul.switchplugin.prefs.util;

import java.util.ArrayList;
import java.util.List;

import org.digitalsoul.switchplugin.actions.Action;
import org.digitalsoul.switchplugin.actions.ActionType;
import org.digitalsoul.switchplugin.actions.FileExtensionAction;
import org.digitalsoul.switchplugin.actions.FilePrefixAction;
import org.digitalsoul.switchplugin.actions.FileSuffixAction;
import org.digitalsoul.switchplugin.actions.FilenameReplacementAction;
import org.digitalsoul.switchplugin.actions.LocationReplacementAction;
import org.digitalsoul.switchplugin.actions.store.FileLoadAction;
import org.digitalsoul.switchplugin.actions.store.FileSaveAction;
import org.digitalsoul.switchplugin.matchers.FileExtensionMatcher;
import org.digitalsoul.switchplugin.matchers.FilePrefixMatcher;
import org.digitalsoul.switchplugin.matchers.FileSuffixMatcher;
import org.digitalsoul.switchplugin.matchers.FilenameMatcher;
import org.digitalsoul.switchplugin.matchers.LocationMatcher;
import org.digitalsoul.switchplugin.matchers.Matcher;
import org.digitalsoul.switchplugin.matchers.MatcherType;
import org.digitalsoul.switchplugin.model.IConfigSwitchItem;
import org.digitalsoul.switchplugin.prefs.wizards.ItemSelectionChangedListener;

public class ItemListConfigurator {

    private ConfigurableItem selectedItem;
    private final List<ConfigurableItem> items;
    private final ItemSelectionChangedListener listener;

    public ItemListConfigurator(List<IConfigSwitchItem> list, ItemSelectionChangedListener listener) {
        this.listener = listener;
        this.items = new ArrayList<ConfigurableItem>();
        for (IConfigSwitchItem item : list) {
            this.items.add(new ConfigurableItem(item));
        }
    }

    public List<IConfigSwitchItem> getItems() {
        List<IConfigSwitchItem> newItems = new ArrayList<IConfigSwitchItem>();
        for (ConfigurableItem item : items) {
            newItems.add(item.getSwitchItem());
        }
        return newItems;
    }

    public void addItem(String name) {
        ConfigurableItem newItem = new ConfigurableItem(name);
        items.add(newItem);
    }

    public void selectItemByName(String name) {
        selectItem(findItem(name));
    }

    private ConfigurableItem selectItem(ConfigurableItem item) {
        this.selectedItem = item;
        listener.itemSelectionChanged();
        return selectedItem;
    }

    public void removeItem(int index) {
        ConfigurableItem item = null;
        items.remove(selectedItem);
        if (items.size() > 0) {
            if (index > 0) {
                index--;
            }
            item = items.get(index);
        }
        selectItem(item);
    }

    private ConfigurableItem findItem(String name) {
        ConfigurableItem foundItem = null;
        for (ConfigurableItem item : items) {
            if (item.getName().equals(name)) {
                foundItem = item;
            }
        }
        return foundItem;
    }

    public String[] getItemNames() {
        String[] names = new String[items.size()];
        int index = 0;
        for (ConfigurableItem item : items) {
            names[index] = item.getName();
            index++;
        }
        return names;
    }

    public boolean hasItemSelection() {
        return selectedItem != null;
    }

    public void removeAction(int selectionIndex) {
        if (hasItemSelection()) {
            selectedItem.removeAction(selectionIndex);
        }
    }

    public void removeMatcher(int selectionIndex) {
        if (hasItemSelection()) {
            selectedItem.removeMatcher(selectionIndex);
        }
    }

    public String[] getConfiguredActionDescriptions() {
        String[] descriptions = null;
        if (hasItemSelection()) {
            descriptions = selectedItem.getActionDescriptions();
        }
        return descriptions;
    }

    public String[] getConfiguredMatcherDescriptions() {
        String[] descriptions = null;
        if (hasItemSelection()) {
            descriptions = selectedItem.getMatcherDescriptions();
        }
        return descriptions;
    }

    public String[] getActionNames() {
        List<String> list = new ArrayList<String>();
        list.add(getName(ActionType.LOCATION_REPLACE));
        list.add(getName(ActionType.FILENAME_REPLACE));
        list.add(getName(ActionType.EXTENSION_REPLACE));
        list.add(getName(ActionType.SUFFIX_APPEND));
        list.add(getName(ActionType.PREFIX_APPEND));
        list.add(getName(ActionType.FILE_SAVE));
        list.add(getName(ActionType.FILE_LOAD));
        return list.toArray(new String[list.size()]);
    }

    public String[] getMatcherNames() {
        List<String> list = new ArrayList<String>();
        list.add(getName(MatcherType.LOCATION));
        list.add(getName(MatcherType.FILENAME));
        list.add(getName(MatcherType.EXTENSION));
        list.add(getName(MatcherType.SUFFIX));
        list.add(getName(MatcherType.PREFIX));
        return list.toArray(new String[list.size()]);
    }

    private String getName(Enum<?> type) {
        return type.name().toLowerCase();
    }

    public void createMatcher(MatcherType type, String str, boolean inverseMatcher) {
        Matcher matcher = null;
        if (type == MatcherType.EXTENSION) {
            matcher = new FileExtensionMatcher(str);
        }
        else if (type == MatcherType.PREFIX) {
            matcher = new FilePrefixMatcher(str);
        }
        else if (type == MatcherType.SUFFIX) {
            matcher = new FileSuffixMatcher(str);
        }
        else if (type == MatcherType.LOCATION) {
            matcher = new LocationMatcher(str);
        }
        else if (type == MatcherType.FILENAME) {
            matcher = new FilenameMatcher(str);
        }
        if (inverseMatcher) {
            matcher.toggleInverse();
        }
        selectedItem.addMatcher(matcher);
    }

    public void createAction(ActionType type, String str1, String str2, boolean inverseAction) {
        Action action = null;
        if (type == ActionType.EXTENSION_REPLACE) {
            action = new FileExtensionAction(str1);
        }
        else if (type == ActionType.SUFFIX_APPEND) {
            action = new FileSuffixAction(str1);
        }
        else if (type == ActionType.PREFIX_APPEND) {
            action = new FilePrefixAction(str1);
        }
        else if (type == ActionType.FILE_SAVE) {
            action = new FileSaveAction(str1);
        }
        else if (type == ActionType.FILE_LOAD) {
            action = new FileLoadAction(str1);
        }
        else if (type == ActionType.LOCATION_REPLACE) {
            action = new LocationReplacementAction(str1, str2);
        }
        else if (type == ActionType.FILENAME_REPLACE) {
            action = new FilenameReplacementAction(str1, str2);
        }
        if (inverseAction) {
            action.toggleInverse();
        }
        selectedItem.addAction(action);
    }

    public boolean hasItem(String name) {
        return findItem(name) != null;
    }

    public void moveActionUp(int selectionIndex) {
        Action action = selectedItem.removeAction(selectionIndex);
        selectedItem.addAction(action, selectionIndex - 1);
    }

    public void moveActionDown(int selectionIndex) {
        Action action = selectedItem.removeAction(selectionIndex);
        selectedItem.addAction(action, selectionIndex + 1);
    }
}
